package com.wteam.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.wteam.PoJo.DO.Address;
import com.wteam.PoJo.DTO.AddressDTO;
import com.wteam.PoJo.DTO.UpdateAddressDTO;
import com.wteam.PoJo.VO.AddressVO;
import com.wteam.mapper.AddressMapper;
import com.wteam.Service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wteam.until.Result;
import com.wteam.until.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 收货地址表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-16
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result saveAddress(AddressDTO addressDTO) {
        //查用户
        Long userid = UserContext.getUser();
        //查是不是有默认，没有强制默认
        Address preaddress = lambdaQuery().eq(Address::getUserId, userid)
                .eq(Address::getIsDefault, 1)
                .one();
        if (preaddress == null) {
            addressDTO.setIsDefault(1);
        }
        Address address = BeanUtil.copyProperties(addressDTO, Address.class);
        address.setUserId(userid);
        //将原有默认改了
        if (address.getIsDefault() == 1) {
            lambdaUpdate().eq(Address::getUserId, userid)
                    .eq(Address::getIsDefault, 1)
                    .set(Address::getIsDefault, 0).update();
        }
        save(address);
        //存redis缓存
        String key = "MyAddress" + "::" + userid;
        redisTemplate.delete(key);
        return Result.success();
    }

    @Override
    public Result updateAddressdefault(Long id) {
        //查用户
        Long userid = UserContext.getUser();

        //改之前的默认为0
        lambdaUpdate().eq(Address::getUserId, userid)
                .eq(Address::getIsDefault, 1)
                .set(Address::getIsDefault, 0)
                .update();
        //加默认
        lambdaUpdate().eq(Address::getId, id)
                .set(Address::getIsDefault, 1)
                .update();
        //删缓存
        String key = "MyAddress" + "::" + userid;
        redisTemplate.delete(key);
        return Result.success();
    }

    @Override
    public Result<List<AddressVO>> getalladdress() {
        //查用户
        Long userid = UserContext.getUser();
        String key = "MyAddress" + "::" + userid;
        //1.先查缓存，看看有没有key
        List<AddressVO> addressVOS1 = (List<AddressVO>) redisTemplate.opsForValue().get(key);
        if (CollUtil.isNotEmpty(addressVOS1) && addressVOS1.size() != 0) {
            return Result.success(addressVOS1);
        }
        //没有正常操作
        List<Address> list = lambdaQuery().eq(Address::getUserId, userid).list();
        List<AddressVO> addressVOS = BeanUtil.copyToList(list, AddressVO.class);
        //存redis
        redisTemplate.opsForValue().set(key, addressVOS);
        return Result.success(addressVOS);
    }

    @Override
    public Result updateaddress(UpdateAddressDTO addressDTO) {
        Address address = BeanUtil.copyProperties(addressDTO, Address.class);

        updateById(address);
        //删缓存
        Long userid = UserContext.getUser();
        String key = "MyAddress" + "::" + userid;
        redisTemplate.delete(key);
        return Result.success();
    }

    @Override
    public Result deletebyID(Long id) {
        Long userid = UserContext.getUser();
        Address address = getById(id);
        removeById(id);
        if (address.getIsDefault() == 1) {
            lambdaUpdate().orderByAsc(Address::getCreateTime)
                    .eq(Address::getUserId, userid)
                    .last("LIMIT 1")
                    .set(Address::getIsDefault, 1).update();
        }
//删除redis
        String key = "MyAddress" + "::" + userid;
        redisTemplate.delete(key);

        return Result.success();
    }

}
